#include "cabinet_dialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QApplication>
#include <QTimer>
#include <QDebug>

CabinetDialog::CabinetDialog(const QString &title, QWidget *parent)
    : QDialog(parent), keyboard(nullptr)
{
    setWindowTitle(title);
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    setWindowState(Qt::WindowFullScreen);
    QVBoxLayout *layout = new QVBoxLayout(this);

    // 创建标题栏布局
    QHBoxLayout *titleLayout = new QHBoxLayout();
    QLabel *titleLabel = new QLabel(title, this);
    titleLabel->setStyleSheet("QLabel { font-size: 24px; font-weight: bold; }");

    // 创建返回按钮
    QPushButton *backButton = new QPushButton("返回", this);
    backButton->setStyleSheet("QPushButton { font-size: 18px; padding: 8px 16px; }");
    connect(backButton, &QPushButton::clicked, this, &QDialog::reject);

    titleLayout->addWidget(titleLabel);
    titleLayout->addStretch();
    titleLayout->addWidget(backButton);

    layout->addLayout(titleLayout);
    layout->addSpacing(20);

    // 创建输入控件
    QLabel *phoneLabel = new QLabel("电话号码：", this);
    phoneEdit = new QLineEdit(this);
    phoneEdit->setPlaceholderText("请输入电话号码");
    phoneEdit->setFocusPolicy(Qt::StrongFocus);

    QLabel *pwdLabel = new QLabel("密码：", this);
    pwdEdit = new QLineEdit(this);
    pwdEdit->setPlaceholderText("请输入密码");
    pwdEdit->setEchoMode(QLineEdit::Password);
    pwdEdit->setFocusPolicy(Qt::StrongFocus);

    QLabel *layerLabel = new QLabel("物理层：", this);
    layerCombo = new QComboBox(this);
    layerCombo->addItem("上");
    layerCombo->addItem("中");
    layerCombo->addItem("下");
    layout->addWidget(layerLabel);
    layout->addWidget(layerCombo);
    // 使用单例获取键盘实例
    keyboard = KeyboardManager::getInstance().getKeyboard(this);

    // 连接键盘信号到输入框
    connect(keyboard, &Keyboard::keyPressed, [=](int key, QString value)
            {
        QWidget *focusWidget = QApplication::focusWidget();
        if (focusWidget && qobject_cast<QLineEdit*>(focusWidget)) {
            QLineEdit *lineEdit = qobject_cast<QLineEdit*>(focusWidget);
            
            // 特殊按键处理
            if (key == Qt::Key_Backspace) {
                lineEdit->backspace();
            } else if (key == Qt::Key_Enter || key == Qt::Key_Return) {
                // 按Enter键时切换焦点或确认
                if (lineEdit == phoneEdit)
                    pwdEdit->setFocus();
                else if (lineEdit == pwdEdit)
                    accept(); // 在密码框按Enter触发确认
            } else {
                // 普通文本输入
                lineEdit->insert(value);
            }
        } });

    // 创建确认按钮
    QPushButton *okBtn = new QPushButton("确认", this);
    okBtn->setStyleSheet("QPushButton { font-size: 18px; padding: 8px 16px; }");

    // 添加控件到布局
    layout->addWidget(phoneLabel);
    layout->addWidget(phoneEdit);
    layout->addWidget(pwdLabel);
    layout->addWidget(pwdEdit);
    layout->addWidget(keyboard); // 添加键盘到布局

    // 创建底部按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(okBtn);
    layout->addLayout(buttonLayout);

    // 设置初始焦点
    phoneEdit->setFocus();

    // 连接确认按钮
    connect(okBtn, &QPushButton::clicked, this, &QDialog::accept);
}

CabinetDialog::~CabinetDialog()
{
    // 归还键盘到永久容器
    KeyboardManager::getInstance().releaseKeyboard();
}

void CabinetDialog::showEvent(QShowEvent *event)
{
    QDialog::showEvent(event);

    // 确保键盘正确显示
    if (keyboard && !keyboard->isVisible())
    {
        keyboard->show();
    }

    // 重新设置焦点
    QTimer::singleShot(100, [this]()
                       { phoneEdit->setFocus(); });
}

void CabinetDialog::closeEvent(QCloseEvent *event)
{
    // 释放键盘回永久容器
    KeyboardManager::getInstance().releaseKeyboard();
    QDialog::closeEvent(event);
}